package cn.iocoder.yudao.module.cms.article.dal.mysql;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.business.basic.dao.mapper.CoreMapper;
import cn.iocoder.yudao.framework.business.basic.dao.query.CoreLambdaQueryWrapper;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.cms.article.dal.dataobject.ArticleDO;
import cn.iocoder.yudao.module.cms.article.vo.AdminArticlePageReqVO;
import cn.iocoder.yudao.module.cms.article.vo.ArticlePageReqVO;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 文章 Mapper
 *
 * @author 山野羡民（1032694760@qq.com）
 */
@Mapper
// 注意避免与 promotion 模块的 ArticleMapper 出现同名 Bean 冲突
public interface ArticleMapper extends CoreMapper<ArticleDO> {

    default PageResult<ArticleDO> selectPage(AdminArticlePageReqVO reqVO) {
        CoreLambdaQueryWrapper<ArticleDO> queryWrapper = new CoreLambdaQueryWrapper<>();
        queryWrapper.eqIfPresent(ArticleDO::getCategoryId, reqVO.getCategoryId());
        queryWrapper.eqIfPresent(ArticleDO::getStatus, reqVO.getStatus());
        queryWrapper.eqIfPresent(ArticleDO::getRecommendHot, reqVO.getRecommendHot());
        queryWrapper.betweenIfPresent(ArticleDO::getCreateTime, reqVO.getCreateTime());
        if (StrUtil.isNotEmpty(reqVO.getKeyword())) {
            queryWrapper.and(e -> e.like(ArticleDO::getTitle, reqVO.getKeyword())
                    .or().like(ArticleDO::getAuthor, reqVO.getKeyword()));
        }
        queryWrapper.orderByDesc(ArticleDO::getId);
        queryWrapper.orderByAsc(ArticleDO::getCategoryId);
        return selectPage(reqVO, queryWrapper);
    }

    default PageResult<ArticleDO> selectPage(ArticlePageReqVO reqVO) {
        CoreLambdaQueryWrapper<ArticleDO> queryWrapper = new CoreLambdaQueryWrapper<>();
        queryWrapper.eqIfPresent(ArticleDO::getCategoryId, reqVO.getCategoryId());
        if (StrUtil.isNotEmpty(reqVO.getKeyword())) {
            queryWrapper.and(e -> e.like(ArticleDO::getTitle, reqVO.getKeyword())
                    .or().like(ArticleDO::getAuthor, reqVO.getKeyword()));
        }
        queryWrapper.orderByAsc(ArticleDO::getCategoryId);
        queryWrapper.orderByDesc(ArticleDO::getId);
        return selectPage(reqVO, queryWrapper);
    }

    default List<ArticleDO> selectList(Boolean recommendHot) {
        return selectList(new CoreLambdaQueryWrapper<ArticleDO>()
                .eqIfPresent(ArticleDO::getRecommendHot, recommendHot)
                .orderByAsc(ArticleDO::getCategoryId)
                .orderByDesc(ArticleDO::getId));
    }

    default List<ArticleDO> selectListByTitle(String title) {
        return selectList(new CoreLambdaQueryWrapper<ArticleDO>()
                .likeIfPresent(ArticleDO::getTitle, title)
                .orderByAsc(ArticleDO::getCategoryId)
                .orderByDesc(ArticleDO::getId));
    }


    default void updateBrowseCount(Long id) {
        update(null, new LambdaUpdateWrapper<ArticleDO>()
                .eq(ArticleDO::getId, id)
                .setSql("browse_count = browse_count + 1"));
    }

}
